from DrissionPage import SessionPage
# from curl_cffi import requests
import time
import requests
import sys
import execjs
from lxml import etree
import execjs._runner_sources as _runner_sources

# 创建 JavaScript 运行时 对象
# 参数中的 command 并不重要，传入空字符串即可
local_node_runtime = execjs.ExternalRuntime(
    name="Node.js (V8) local",
    command='',
    encoding='UTF-8',
    runner_source=_runner_sources.Node
)
# 这里是重点，需要强制性修改
local_node_runtime._binary_cache = ['D:/软件管理/node/node.exe']
local_node_runtime._available = True
# 将刚创建好的 JavaScript 运行时 注册至 PyExecJS 中
execjs.register('local_node', local_node_runtime)


def text():
    try:
        # ipconfig = IpConfig()
        # proxy = ipconfig.get()
        # proxies = {'http': proxy['http'], 'https': proxy['https']}
        # print(proxies)

        proxies = {
            "http": "n642.kdltpspro.com:15818",
            "https": "n642.kdltpspro.com:15818"
        }
        session = requests.session()
        # proxies = {'http': 'http://122.193.121.229:24046', 'https': 'http://122.193.121.229:24046'}

        headers = {
            "authority": "www.haodf.com",
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "pragma": "no-cache",
            "referer": "https://www.haodf.com/doctor/6964556758.html?decode__1282=eq0x9D0DyDcAI8DBqo53GQGCWD8KYHKDuW%3DYD",
            "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "document",
            "sec-fetch-mode": "navigate",
            "sec-fetch-site": "same-origin",
            "sec-fetch-user": "?1",
            "upgrade-insecure-requests": "1",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36"
        }
        url = "https://www.haodf.com/doctor/9032010964/xinxi-jieshao.html"

        ctx = execjs.get('local_node').compile(open('./decode__1282.js', 'r', encoding='utf-8').read()).call(
            'main', url)
        print(ctx)

        params = {
            "decode__1282": f"{ctx}"
        }
        response = session.get(url, headers=headers, params=params,timeout=2,proxies=proxies)
        html = response.text
        if '验证' in html:
            print('出验证了===')
            return text()
        else:
            tree = etree.HTML(html)
            xm = "".join(tree.xpath('/html/body/div[1]/div/div[2]/div[2]/h1/text()'))
            print(xm)
        return 200
    except Exception as e:
        print('错误：',e)
        return text()

for _ in range(10):
    start = time.time()
    text()
    end = time.time()
    print('用时===', str(end - start))
    print('==============================================================================')